package com.ce.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.ce.utils.DateUtils;


public class Order  implements Serializable{

	private static final long serialVersionUID = -4428177456839280216L;
	
	private String time;
	
	private String orderNo;
	
	private String address;
	
	private String phone;
	
	private float price;
	
	private Map<String, OrderDetail> orderDetails = new HashMap<String, OrderDetail>();

	public Map<String, OrderDetail> getOrderDetails() {
		return orderDetails;
	}
	
	public Order() {
		time = DateUtils.getDateToStr(new Date(), "yyyy-MM-dd");
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}
	
	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public void addOrderDetail(OrderDetail detail) {
		if(detail == null)
			return;
		String key = detail.getKey();
		if(orderDetails.containsKey(key)) {
			OrderDetail theDetail = orderDetails.get(key);
			theDetail.setNumber(theDetail.getNumber() + 1);
		}else {
			orderDetails.put(key, detail);
		}
	}
	
	public String getOrderTime() {
		return time;
	}
	
	public String getOrderJson(Customer customer) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("{")
		   .append("\"orderNo\":\"").append(orderNo).append("\"")
		   .append(",\"userId\":\"").append(customer.getAutoId()).append("\"")
		   .append(",\"userName\":\"").append(customer.getUserName()).append("\"")
		   .append(",\"userAddress\":\"").append(address).append("\"")
		   .append(",\"userCode\":\"").append("").append("\"")
		   .append(",\"userPhone\":\"").append(phone).append("\"")
		   .append(",\"userMail\":\"").append(customer.getUserMail()).append("\"")
		   .append(",\"ordertime\":\"").append(time).append("\"")
		   .append(",\"payType\":\"").append("").append("\"")
		   .append(",\"camiedType\":\"").append("").append("\"")
		   .append(",\"result\":\"").append("").append("\"")
		   .append(",\"approvedUserId\":\"").append("").append("\"")
		   .append(",\"approvedDate\":\"").append("").append("\"")
		   .append(",\"approvedUserName\":\"").append("").append("\"")
		   .append(",\"community\":\"").append("").append("\"")
		   .append(",\"station\":\"").append("").append("\"") 
		   .append(",\"exit\":\"").append("").append("\"")
		   .append(",\"totalPrice\":\"").append(price).append("\"")
		   .append("}");
		return sbf.toString();
	}
	
	public String getDetailJson() {
		StringBuffer sbf = new StringBuffer("[");
		String split = "";
		OrderDetail detail = null;
		for(String key : orderDetails.keySet()) {
			detail = orderDetails.get(key);
			sbf.append(split);
			sbf.append("{")
			   .append("\"itemId\":\"").append(detail.getMDish().getAutoId()).append("\",")
			   .append("\"itemName\":\"").append(detail.getMDish().getTitle()).append("\",")
			   .append("\"itemCount\":\"").append(detail.getNumber()).append("\",")
			   .append("\"price\":\"").append(detail.getNumber() * detail.getMDish().getPrice()).append("\"")
			   .append("}");
			   split = ",";
		}
		sbf.append("]");
		if(sbf.length() == 2)
			return "";
		return sbf.toString();
	}
}
